package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/address")
@CrossOrigin
public class AddressController {


    @Reference
    private AddressService addressService;


    //分页查询分院信息
    @GetMapping(path = "/findAll")
    public Result findAll(){
        try {
            List<Address> pageResult = addressService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //编辑分院信息
    @PutMapping(path = "/update")
    public Result update(@RequestBody Address address) {
        try {
            addressService.update(address);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }



    //更新是否启用按钮
    @PutMapping(path = "/updateEnableById")
    public Result updateEnableById(Integer id,Integer enable){
        try {
            addressService.updateEnableById(id,enable);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }







    //查询检查组基本信息
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Address address = addressService.findById(id);
            return new Result(true,  MessageConstant.QUERY_ADDRESS_SUCCESS, address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }



    //添加分院信息
    @PostMapping(path = "/submit")
    public Result submit(@RequestBody Address address){

        try {
            addressService.submit(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }











}
